//
//  YJDUserViewController.m
//  YJDSDK
//
//  Created by skyline on 2018/3/6.
//  Copyright © 2018年 skyline. All rights reserved.
//



#import "YJDUserTipView.h"
#import "YJDUserPortrait.h"
#import "YJDUserOption.h"
#import "YJDPDF.h"
#import "YJDUserViewController.h"
#import "YJDPWDViewController.h"
@interface YJDUserViewController ()<YJDUserPortraitDelegate>
@property (nonatomic, strong) YJDUserTipView *tipsView;
@property (nonatomic, strong) YJDUserPortrait *portraitView;
@property (nonatomic, strong) YJDPDF *pdfView;
@end

@implementation YJDUserViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.view.backgroundColor = CELL_LINE_COLOR;
    
    self.portraitView.hidden = NO;
    self.naviBar.title = @"用户中心";
    // Do any additional setup after loading the view.
}

- (YJDUserTipView *)tipsView
{
    if (!_tipsView)
    {
        _tipsView = [[YJDUserTipView alloc] initWithText:@"建议尽快绑定账号，以防数据丢失"];
        [self.view addSubview:_tipsView];
        [_tipsView mas_makeConstraints:^(SK_MASConstraintMaker *make) {
            make.left.equalTo(self.view);
            make.right.equalTo(self.view);
            make.top.equalTo(self.naviBar.mas_bottom);
            make.height.mas_equalTo(25);
        }];
    }
    return _tipsView;
}

- (YJDUserPortrait *)portraitView
{
    if (!_portraitView)
    {
        _portraitView = [[YJDUserPortrait alloc] initWithName:[YJDPlatform platform].accountID];
        _portraitView.delegate = self;
        [self.view addSubview:_portraitView];
        [_portraitView mas_makeConstraints:^(SK_MASConstraintMaker *make) {
            make.left.equalTo(self.view);
            make.right.equalTo(self.view);
            make.height.mas_equalTo(HEIGHT);
            if (![YJDHelper shareHelp].isBind)
                make.top.equalTo(self.tipsView.mas_bottom);
            else
                make.top.equalTo(self.naviBar.mas_bottom);
        }];
    }
    return _portraitView;
}

- (YJDPDF *)pdfView
{
    if (!_pdfView)
    {
        _pdfView = [[YJDPDF alloc] initWithPDF:nil];
        [self.view addSubview:_pdfView];
        [_pdfView mas_makeConstraints:^(SK_MASConstraintMaker *make) {
            make.edges.mas_offset(UIEdgeInsetsZero);
        }];
    }
    return _pdfView;
}

- (void)yjd_setupSubView
{
    
    NSString *bind = ([YJDHelper shareHelp].isBind)?@"已绑定":@"绑定手机";
    NSString *authenticate = ([YJDHelper shareHelp].isAuthenticate)?@"已认证":@"实名认证";
    
    NSArray *titleArr = @[bind, authenticate,@"修改密码", @"我的消息", @"用户协议", @"用户帮助"];
    NSArray *iconArr = @[@"pwdInput",@"pwdInput",@"pwdInput", @"news", @"help", @"xieyi"];
    for (int i = 0; i < titleArr.count; i++)
    {
        int row = i / 2;
        int column = i % 2;
        YJDUserOption *option = [[YJDUserOption alloc] initWithTitle:titleArr[i] icon:iconArr[i]];
        [option addTarget:self action:@selector(yjd_actionForOption:) forControlEvents:UIControlEventTouchUpInside];
        option.tag = 200 + i;
        
        if ([YJDHelper shareHelp].isBind && i == 0)
        {
            option.titileColor = [YJDHelper shareHelp].color;
        }
        else if ([YJDHelper shareHelp].isAuthenticate && i == 1)
        {
            option.titileColor = [YJDHelper shareHelp].color;
        }
        
        [self.view addSubview:option];
        [option mas_makeConstraints:^(SK_MASConstraintMaker *make) {
            make.top.equalTo(self.portraitView.mas_bottom).with.offset(GAP + (USEROPTIONHEIGHT + 1) * row);
            make.height.mas_equalTo(USEROPTIONHEIGHT);
            if (column % 2)
            {
                make.right.equalTo(self.view);
                make.left.equalTo(self.view.mas_centerX).with.offset(0.5);
            }
            else
            {
                make.left.equalTo(self.view);
                make.right.equalTo(self.view.mas_centerX).with.offset(-0.5);
            }
        }];
    }
}

- (void)yjd_actionForOption:(YJDUserOption *)sender
{    
    if (sender.tag == 200)
    {
        if ([YJDHelper shareHelp].isBind)
        {
            [YJDAlert yjd_showInWindowWithTitle:@"提示" message:@"此账号已绑定手机" complete:nil];
            return;
        }
        return [self presentViewController:[NSClassFromString(@"YJDBindViewController") new] animated:YES completion:nil];
    }
    else if (sender.tag == 201)
    {
        if ([YJDHelper shareHelp].isAuthenticate)
        {
            [YJDAlert yjd_showInWindowWithTitle:@"提示" message:@"此账号已实名认证" complete:nil];
            return;
        }
        return [self presentViewController:[NSClassFromString(@"YJDRZViewController") new] animated:YES completion:nil];
    }
    else if (sender.tag == 202)
    {
        YJDPWDViewController *pwd = [[YJDPWDViewController alloc] init];
        pwd.type = YJDPWDViewTypeResetPWD;
        [self presentViewController:pwd animated:YES completion:nil];
    }
    else if (sender.tag == 203)
    {
        [YJDAlert yjd_showInWindowWithTitle:@"提示" message:@"此功能暂未开放" complete:nil];
        return;
        [self presentViewController:[NSClassFromString(@"YJDNewsViewController") new] animated:YES completion:nil];
    }
    else if (sender.tag == 204)
    {
        self.pdfView.pdf = @"protocol";
    }
    else if (sender.tag == 205)
    {
        self.pdfView.pdf = @"userHelp";
    }
}

#pragma mark - YJDUserPortraitDelegate 方法
- (void)yjd_methodForSwitchButton
{
    [self dismissViewControllerAnimated:NO completion:^{
        if (self.delegate && [self.delegate respondsToSelector:@selector(yjd_YJDUserViewControllerDelegate)])
        {
            [self.delegate yjd_YJDUserViewControllerDelegate];
        }
    }];
}

- (void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];
    [self yjd_setupSubView];
}

- (void)viewDiddAppear:(BOOL)animated
{
    [super viewDidAppear:animated];
}

- (void)viewDidDisappear:(BOOL)animated
{
    [super viewDidDisappear:animated];
    for (UIView *view in [self.view subviews])
    {
        if (view.tag >= 200)
        {
            [view removeFromSuperview];
        }
    }
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
 #pragma mark - Navigation
 
 // In a storyboard-based application, you will often want to do a little preparation before navigation
 - (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
 // Get the new view controller using [segue destinationViewController].
 // Pass the selected object to the new view controller.
 }
 */

@end
